package net.ib.mtalk.network.task;

import net.ib.mntalk.protocol.Common;
import net.ib.mntalk.protocol.Common.BuddyInfo;
import net.ib.mntalk.protocol.Common.UUIDMsg;
import net.ib.mntalk.protocol.Common.UUIDSlicePredicate;
import net.ib.mntalk.protocol.Common.UserBasicInfo;
import net.ib.mntalk.protocol.Session.GetBuddyInfosReq;
import net.ib.mntalk.protocol.Session.GetMembersReq;
import net.ib.mntalk.protocol.Session.GetMembersRes;
import net.ib.mntalk.protocol.client.Database.ReceiverList;
import net.ib.mtalk.constant.HandlerMsgConst;
import net.ib.mtalk.database.helper.InboxDatabaseHelper;
import net.ib.mtalk.network.MethodId.Session;
import net.ib.mtalk.network.TcpEnvelope;
import net.ib.mtalk.util.GpbSupport;
import net.ib.mtalk.util.MTalkLogWriter;
import android.content.Context;
import android.os.Handler;
import android.util.Log;

public class GetMembersTask extends AsyncNetTask {
	
	private long mClientViewUuidMsb;
	private long mClientViewUuidLsb;
	private GetMembersReq mGetMembersReq;
	
	public GetMembersTask(Context context, Handler handler, long clientViewUuidMsb, long clientViewUuidLsb, GetMembersReq getMembersReq){
		super(context, handler);
		this.mGetMembersReq = getMembersReq;
		this.mClientViewUuidMsb = clientViewUuidMsb;
		this.mClientViewUuidLsb = clientViewUuidLsb;
	}
	
	@Override
	protected TcpEnvelope onPreExecute() {
		TcpEnvelope rev = new TcpEnvelope.Builder()
			.setType(TcpEnvelope.REQUEST)
			.setMessage(mGetMembersReq)
			.setMethodId(Session.View.GetMembers)
			.build();
		return rev;
	}
	
	@Override
	protected void onPostExecute(int resultCode, Object sentObject,
			Object receivedObject) {
		MTalkLogWriter.d("GetMembersTask.onPostExecute() resultCode : " + resultCode);
		if (receivedObject == null) {
			mUiHandler.sendMessageAtFrontOfQueue(mUiHandler.obtainMessage(HandlerMsgConst.HANDLER_UNKNOWN_ERROR));
			return;
		}
		
		TcpEnvelope resultEnvelope = (TcpEnvelope) receivedObject;
		GetMembersRes getMembersRes = (GetMembersRes) resultEnvelope.getMessage();
		
		MTalkLogWriter.d("GetMembersTask ResponseCode : " + getMembersRes.getResponseCode());
		
		if(getMembersRes.getResponseCode() == Common.SUCCESS){
			
			if(getMembersRes.getReceiverCount() > 0){
				ReceiverList receiverList = new ReceiverList();
				
				for(UserBasicInfo info : getMembersRes.getReceiverList()){
					UserBasicInfo userBasicInfo = new UserBasicInfo();
					userBasicInfo.setDisplayName(info.getDisplayName());
					userBasicInfo.setUserUuid(info.getUserUuid());
					receiverList.addReceiver(userBasicInfo);
				}
				
				// DB에 멤버를 업데이트 한다.
				InboxDatabaseHelper.setMemberList(mContext, mClientViewUuidMsb, mClientViewUuidLsb, receiverList);
			}
			mUiHandler.sendMessageAtFrontOfQueue(mUiHandler.obtainMessage(HandlerMsgConst.HANDLER_GET_MEMBERS_SUCCESS));
		} else{
			mUiHandler.sendMessageAtFrontOfQueue(mUiHandler.obtainMessage(HandlerMsgConst.HANDLER_GET_MEMBERS_FAIL));
		}
	}
}